package xjtu.zd.server.staff.data.impl;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xjtu.zd.server.staff.data.action.DataAction;

import xjtu.zd.server.staff.data.DataService;
import xjtu.zd.server.staff.data.action.DateAction;
import xjtu.zd.server.staff.data.dao.DataDao;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class DataServiceImpl implements DataService {
    @Resource
    private DataDao dataDao;

    private Map<String,Object> getData30d(DataAction dataAction){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date=new Date();
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        calendar.setTime(date);
        List<String> strlist = new ArrayList<>();
        List<String> countlist = new ArrayList<String>();
        int i=0;
        while(i<30) {
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            date = calendar.getTime();
            strlist.add(sdf.format(date));
            i++;
        }
        Collections.reverse(strlist);
        for(int j=0;j<strlist.size();j++){
            //System.out.println(strlist.get(j));
            countlist.add(dataAction.getNewData(strlist.get(j)).toString());
        }
        Map<String,Object> page = new HashMap<>();
        page.put("datelist",strlist);
        page.put("countlist",countlist);
        return page;
    }

    private Map<String, Object> getData12m(DataAction dataAction){
        java.util.Calendar cal = java.util.Calendar.getInstance();
        int nowyear = cal.get(Calendar.YEAR);
        int nowmonth = cal.get(Calendar.MONTH)+1;

        String mon = "";
        List<String> countlist = new ArrayList<String>();
        List<String> strlist = new ArrayList<String>();
        int a = 0;
        while (a<12){
            if(nowmonth-a==0){
                nowmonth = 12+a;
                nowyear = nowyear-1;
            }
            mon = (nowmonth-a) <10 ? ("0"+(nowmonth-a)) : (""+(nowmonth-a));
            System.out.println(nowyear+"-"+mon);
            strlist.add(nowyear+"-"+mon);
            a++;
        }
        Collections.reverse(strlist);
        for(int i=0; i<strlist.size();i++) {
            System.out.println(strlist.get(i));
            countlist.add(dataAction.getNewData(strlist.get(i)).toString());
        }
        Map<String,Object> page = new HashMap<>();
        page.put("datelist",strlist);
        page.put("countlist",countlist);
        return page;
    }

    private Map<String, Object> getAllData(DateAction dateAction,DataAction dataAction){
        java.util.Calendar cal = java.util.Calendar.getInstance();

        int nowyear = cal.get(Calendar.YEAR);
        int nowmonth = cal.get(Calendar.MONTH)+1;

        String[] earliest =dateAction.getEarDate().split("-");
        int earyear = Integer.parseInt(earliest[0]);
        int earmonth = Integer.parseInt(earliest[1]);

        String mon = "";
        List<String> countlist = new ArrayList<String>();
        List<String> strlist = new ArrayList<String>();

        for(int i=0;earyear+i<=nowyear;i++) {
            for (int j = 0; (earmonth + j <= 12) && (earyear < nowyear) && (i == 0); j++) {
                mon = (earmonth + j) < 10 ? ("0" + (earmonth + j)) : ("" + (earmonth + j));
                strlist.add((earyear + i) + "-" + mon);
            }
            for (int k = 1; (k <= 12) && (earyear + i < nowyear) && (i > 0); k++) {
                mon = k < 10 ? ("0" + k) : ("" + k);
                strlist.add((earyear + i) + "-" + mon);
            }
            for (int l = 1; (l <= nowmonth) && (earyear + i == nowyear) && (earyear < nowyear); l++) {
                mon = l < 10 ? ("0" + l) : ("" + l);
                strlist.add((earyear + i) + "-" + mon);
            }
            for (int m = 0; (earyear == nowyear) && (earmonth + m <= nowmonth); m++) {
                mon = (earmonth + m) < 10 ? ("0" + (earmonth + m)) : ("" + (earmonth + m));
                strlist.add((earyear + i) + "-" + mon);
            }
        }


        for(int i=0; i<strlist.size();i++) {
            countlist.add(dataAction.getNewData(strlist.get(i)).toString());
        }
        Map<String,Object> page = new HashMap<>();
        page.put("datelist",strlist);
        page.put("countlist",countlist);
        return page;
    }

    @Override
    public Map<String, Object> userDataPermon() {
        return getAllData(() -> dataDao.getEarUserDate(),a ->dataDao.getNewUserData(a));
    }
    @Override
    public Map<String, Object> bookDataPermon() {
        return getAllData(() -> dataDao.getEarBookDate(),a ->dataDao.getNewBookData(a));
    }


    @Override
    public Map<String, Object> userData12m() {
        return getData12m(a -> dataDao.getNewUserData(a));
    }
    @Override
    public Map<String, Object> bookData12m() {
        return getData12m(a -> dataDao.getNewBookData(a));
    }


    @Override
    public Map<String, Object> activeAmon() { return getData30d(a -> dataDao.getActiveUser(a)); }

    /*@Override
    public Map<String, Object> userDataPermon() {
        List<String> countlist = new ArrayList<String>();
        List<String> strlist = this.getDateList(() -> dataDao.getEarUserDate());
        for(int i=0; i<strlist.size();i++){
            countlist.add(dataDao.getNewUserData(strlist.get(i)).toString());
        }
        Map<String,Object> page = new HashMap<>();
        page.put("datelist",strlist);
        page.put("countlist",countlist);
        return page;
    }

    @Override
    public Map<String,Object> userDataPermon() {
        DataDto dto = new DataDto("t_user","u_id");
        dataDao.getNewData(dto);
        java.util.Calendar cal = java.util.Calendar.getInstance();

        int nowyear = cal.get(Calendar.YEAR);
        int nowmonth = cal.get(Calendar.MONTH)+1;

        String[] earliest = (dataDao.getEarUserDate()).split("-");
        int earyear = Integer.parseInt(earliest[0]);
        int earmonth = Integer.parseInt(earliest[1]);

        String mon = "";
        List<String> countlist = new ArrayList<String>();
        List<String> strlist = new ArrayList<String>();


        for(int i=0;earyear+i<=nowyear;i++) {
            for (int j = 0; (earmonth+j<=12)&&(earyear<nowyear)&&(i==0); j++) {
                mon = (earmonth + j)<10?("0"+(earmonth + j)):(""+(earmonth + j));
                countlist.add(dataDao.getNewUserData((earyear + i)+mon).toString());
                strlist.add((earyear + i)+"-"+mon);
            }
            for (int k = 1; (k<=12)&&(earyear+i<nowyear)&&(i>0); k++) {
                mon = k<10?("0"+k):(""+k);
                countlist.add(dataDao.getNewUserData((earyear + i)+mon).toString());
                strlist.add((earyear + i)+"-"+mon);
            }
            for (int l = 1; (l<=nowmonth)&&(earyear+i==nowyear)&&(earyear<nowyear); l++){
                mon = l<10?("0"+l):(""+l);
                countlist.add(dataDao.getNewUserData((earyear + i)+mon).toString());
                strlist.add((earyear + i)+"-"+mon);
            }
            for (int m = 0; (earyear==nowyear)&&(earmonth+m<=nowmonth); m++){
                mon = (earmonth + m)<10?("0"+(earmonth + m)):(""+(earmonth + m));
                countlist.add(dataDao.getNewUserData((earyear + i)+mon).toString());
                strlist.add((earyear + i)+"-"+mon);
            }
        }

        Map<String,Object> page = new HashMap<>();
        page.put("datelist",strlist);
        page.put("countlist",countlist);


        return page;
    }*/
}
